﻿''' <summary>
''' Contains the list of command results for each command processed since the interpreter began, or since the clear command was last executed, along with the combined log entries from each result.
''' </summary>
''' <remarks></remarks>
Public Class CommandResultCollection
    Implements IEnumerable(Of CommandResult)

    Private _ItemsCore As New List(Of CommandResult)
    Private _IdKeys As New List(Of Guid)
    Private _LineKeys As New List(Of CommandLine)

    Default Public ReadOnly Property Item(index As Integer) As CommandResult
        Get
            Return _ItemsCore(index)
        End Get
    End Property

    Default Public ReadOnly Property Item(id As Guid) As CommandResult
        Get
            Return _ItemsCore(_IdKeys.IndexOf(id))
        End Get
    End Property

    Default Public ReadOnly Property Item(line As CommandLine) As CommandResult
        Get
            Return _ItemsCore(_LineKeys.IndexOf(line))
        End Get
    End Property

    Private _LogEntries As New List(Of LogListEntry)
    Public ReadOnly Property LogEntries As LogListEntry()
        Get
            Return _LogEntries.ToArray
        End Get
    End Property

    Public Function Contains(id As Guid) As Boolean
        Return _IdKeys.Contains(id)
    End Function

    Public Function Contains(line As CommandLine) As Boolean
        Return _LineKeys.Contains(line)
    End Function

    Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of CommandResult) Implements System.Collections.Generic.IEnumerable(Of CommandResult).GetEnumerator
        Return _ItemsCore.GetEnumerator
    End Function

    Protected Function GetEnumeratorBase() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
        Return GetEnumerator()
    End Function

    Protected Friend Sub Clear()
        _ItemsCore.Clear()
        _IdKeys.Clear()
        _LineKeys.Clear()
        _LogEntries.Clear()
    End Sub

    Protected Friend Sub Add(item As CommandResult)
        _ItemsCore.Add(item)
        _IdKeys.Add(item.Id)
        _LineKeys.Add(item.CommandLine)
        For Each logitem As LogEntry In item._Log
            _LogEntries.Add(New LogListEntry(logitem, item))
        Next
    End Sub
End Class

Public Structure LogListEntry
    Public LogEntry As LogEntry
    Public Result As CommandResult

    Public Sub New(entry As LogEntry, fromResult As CommandResult)
        LogEntry = entry
        Result = fromResult
    End Sub
End Structure